<?php 

// no direct access
defined ( '_JEXEC' ) or die ( 'Restricted access' );

if( !class_exists('OLImage') ){

jimport('joomla.filesystem.file');

class OLImage {
var $__cachePath;
var $__imageBase;
var $types = array();
var $__quality = 90;
var $__cacheURL;
function __construct(){
$this->types = array( 1 => "gif", "jpeg", "png", "swf", "psd", "wbmp" );	
$this->__imageBase = JPATH_SITE.DS.'images'.DS;
$this->__cachePath = $this->__imageBase.'resized'.DS;
$this->__cacheURL = 'images/resized/';
}
/**
* constructor 
*/
function OLImage(){
$this->__construct();
}

function &getInstance(){
static $instance = null;
if( !$instance ){
$instance = new OLImage();
}
return $instance;
}


function resize( $image, $width, $height, $crop=true,  $aspect=true ){
// get image information
if( !$width || !$height ) return '';

$imagSource = JPATH_SITE.DS. str_replace( '/', DS,  $image );
if( !file_exists($imagSource) || !is_file($imagSource) ){ return ''; }
$size = getimagesize( $imagSource );
// if it's not a image.
if( !$size ){ return ''; }

// case 1: render image base on the ratio of source.
$x_ratio = $width / $size[0];
$y_ratio = $height / $size[1];

// set dst, src
$dst = new stdClass(); 
$src = new stdClass();
$src->y = $src->x = 0;
$dst->y = $dst->x = 0;

if ($width > $size[0])
$width = $size[0];
if ($height > $height)
$height = $size[1];


if ( $crop ) {	// processing crop image 	
$dst->w = $width;
$dst->h = $height;
if ( ($size[0] <= $width) && ($size[1] <= $height) ) {
$src->w = $width;
$src->h = $height;
} else {
if ($x_ratio < $y_ratio) {
$src->w = ceil ( $width / $y_ratio );
$src->h = $size[1];
} else {
$src->w = $size[0];
$src->h = ceil ( $height / $x_ratio );
}
}
$src->x = floor ( ($size[0] - $src->w) / 2 );
$src->y = floor ( ($size[1] - $src->h) / 2 );
} else { // processing resize image.
$src->w = $size[0];
$src->h = $size[1];
if( $aspect ) { // using ratio
if ( ($size[0] <= $width) && ($size[1] <= $height) ) {
$dst->w = $size[0];
$dst->h = $size[1];
} else if ( ($size[0] <= $width) && ($size[1] <= $height) ) {
$dst->w = $size[0];
$dst->h = $size[1];
} else if ( ($x_ratio * $size[1]) < $height ) {
$dst->h = ceil ( $x_ratio * $size[1] );
$dst->w = $width;
} else {
$dst->w = ceil ( $y_ratio * $size[0] );
$dst->h = $height;
}
} else { // resize image without the ratio of source.
$dst->w = $width;
$dst->h = $height;
}
}
//
$ext =	substr ( strrchr ( $image, '.' ), 1 ); 
$thumnail =  substr ( $image, 0, strpos ( $image, '.' )) . "_{$width}_{$height}.".$ext; 
$imageCache   = $this->__cachePath .  str_replace( '/', DS, $thumnail );

if( file_exists($imageCache) ){
$smallImg = getimagesize ( $imageCache );
if ( ($smallImg [0] == $dst->w && $smallImg [1] == $dst->h)  ) {
return  $this->__cacheURL. $thumnail;
}
} 

if( !file_exists($this->__cachePath) && !mkdir($this->__cachePath) ) {
return '';
}

if( !$this->makeDir( $image ) ) {
return '';
}

// resize image
$this->_resizeImage( $imagSource, $src, $dst, $size, $imageCache ); 

return  $this->__cacheURL. $thumnail;					
}

function resizeLinkedImage( $url, $host ){

if( !is_dir($this->__imageBase . DS . 'linked_images' . DS) ){
if( !mkdir($this->__imageBase . DS . 'linked_images' . DS, 0755) ){
return '';
}
}
//	mkdir($this->__imageBase . DS . 'linked_images'.DS . $host['host'] . DS, 0755);
//
$filePath = $this->__imageBase. 'linked_images/'.$host['host'].'/' .'testthu.jpg';
JFile::exists( $filePath );
$source = file_get_contents( $url ); 
JFile::write( $filePath, $source );
$files = 'images/linked_images/'.$host['host'].'/testthu.jpg';

$output = $this->resize( $files, 160, 80 );

//	if( $this->_storeImage ){
//	JFile::delete( $filePath  );
//	}

return $output;	
}


function makeDir( $path ){
$folders = explode ( '/',  ( $path ) );
$tmppath = $this->__cachePath;	
for( $i = 0; $i < count ( $folders ) - 1; $i ++) {
if (! file_exists ( $tmppath . $folders [$i] ) && ! mkdir ( $tmppath . $folders [$i], 0755) ) {
return false;
}	
$tmppath = $tmppath . $folders [$i] . DS;
}		
return true;
}

function _resizeImage( $imageSource, $src, $dst, $size, $imageCache ){
// create image from source.
$extension = $this->types[$size[2]];
$image = call_user_func( "imagecreatefrom".$extension, $imageSource );

if( function_exists("imagecreatetruecolor") && ($newimage = imagecreatetruecolor($dst->w, $dst->h)) ){

if( $extension == 'gif' || $extension == 'png' ){
imagealphablending ( $newimage, false );
imagesavealpha ( $newimage, true );
$transparent = imagecolorallocatealpha ( $newimage, 255, 255, 255, 127 );
imagefilledrectangle ( $newimage, 0, 0, $dst->w, $dst->h, $transparent );
}

imagecopyresampled ( $newimage, $image, $dst->x, $dst->y, $src->x, $src->y, $dst->w, $dst->h, $src->w, $src->h );
} else {
$newimage = imagecreate ( $width, $height );
imagecopyresized ( $newimage, $image, $dst->x, $dst->y, $src->x, $src->y, $dst->w, $dst->h, $size[0], $size[1] );
}

switch( $extension ){
case 'jpeg' :
call_user_func( 'image'.$extension, $newimage, $imageCache, $this->__quality );	
break;
default:
call_user_func( 'image'.$extension,$newimage, $imageCache );
break;	
}
// free memory
imagedestroy ( $image );
imagedestroy ( $newimage );
}

/**
* set quality image will render.
*/
function setQuality( $number = 9 ){
$this->__quality = $number;
}

function isLinkedImage( $imageURL ){
$parser = parse_url($imageURL);
return  strpos( JURI::base (), $parser['host'] ) ?false:$parser;
}

function isImage( $ext = '' ){
return in_array($ext, $this->types);
}

function sourceExited( $imageSource ) {

if( $imageSource == '' || $imageSource == '..' || $imageSource == '.' ){
return false;
}
$imageSource = str_replace ( JURI::base (), '', $imageSource );
$imageSource = rawurldecode ( $imageSource );
return ( file_exists ( JPATH_SITE . '/' . $imageSource ) );	
}

function parseImage( $text ) {
$regex = "/\<img.+src\s*=\s*\"([^\"]*)\"[^\>]*\>/";
preg_match ( $regex, $text, $matches );
$images = (count ( $matches )) ? $matches : array ();
$image = count ( $images ) > 1 ? $images [1] : '';
return $image;
}
}
}
?>